//
//  CTHomePageTableView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/5.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTHomePageTableView.h"
#import "CTDebugManager.h"
#import "RefreshControl.h"
#import "CTHeader.h"
#import "CTHotSpotDetailCell.h"
#import "CTDeviceFunction.h"
#import "CTDiscoveryItems.h"
#import "CTSubjectCell.h"
#import "CTHomePageItems.h"
#import "CTUser.h"
#import "CTFile.h"
#import "CTDeviceFunction.h"


@interface CTHomePageTableView ()<RefreshControlDelegate,UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,CTHotSpotDetailCellDelegate,CTSubjectCellDelegate>
{
    UITableView * homePageTableView;
    RefreshControl * refreshControl;
    
    NSMutableArray  *hotSpotsArray;
    CTSubjectItem * subjectItm;
}
@end


@implementation CTHomePageTableView

@synthesize delegate,currentHotSpotsItem;

- (void)dealloc
{
    [homePageTableView removeFromSuperview];
    homePageTableView = nil;
    currentHotSpotsItem = nil;
    refreshControl = nil;
    hotSpotsArray = nil;
    
    subjectItm = nil;
    
    delegate = nil;
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTHomePageTableView Is Removed!!!"];
}



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        [self creatSubviews];
    }
    return self;
}

-(void)creatSubviews
{
    
    
    
    homePageTableView  = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    homePageTableView .delegate = self;
    homePageTableView .backgroundColor = [UIColor clearColor];
    homePageTableView .dataSource = self;
//    homePageTableView .rowHeight = 140.0;
    homePageTableView.showsVerticalScrollIndicator = NO;
    homePageTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    homePageTableView .autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [homePageTableView  setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self addSubview:homePageTableView ];
    
    
    /*
     *** refresh control
     */
    refreshControl = [[RefreshControl alloc] initWithScrollView:homePageTableView delegate:self];
    refreshControl.topEnabled = YES;
    refreshControl.bottomEnabled = YES;
    refreshControl.enableInsetBottom = 60;
    
}



#pragma mark - tableView delegate -



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 1;
    }
    if (section == 1)
    {
        return hotSpotsArray.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSInteger section = indexPath.section;
    if (section == 0) {
        
        static NSString *cellIdentifier = @"Cell_Subject";
        CTSubjectCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[CTSubjectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            cell.delegate = self;
        }
        cell.item = subjectItm;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        homePageTableView.rowHeight = cell.bounds.size.height;

        return cell;
        
    }
    else
    {
        static NSString *cellIdentifier = @"Cell";
        CTHotSpotDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[CTHotSpotDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            cell.delegate = self;
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        CTHotSpotsDetailItem * item = (CTHotSpotsDetailItem *)[hotSpotsArray objectAtIndex:indexPath.row];
        cell.hotItem = item;

        
        
        return cell;
    }

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CTHotSpotDetailCell *cell = (CTHotSpotDetailCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}




#pragma mark - action & delegate -
-(void)refresh
{
    [refreshControl startRefreshingDirection:RefreshDirectionTop];
}

-(void)refreshPullDownEnd
{
    [refreshControl finishRefreshingDirection:RefreshDirectionTop];
}

-(void)refreshPullUpEnd
{
    [refreshControl finishRefreshingDirection:RefreshDirectionBottom];
}

-(void)reloadData
{
    [homePageTableView reloadData];
}

#pragma mark - refresh control delegate -

- (void)refreshControl:(RefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection) direction
{
    if (direction == RefreshDirectionTop) {
        if ([self.delegate respondsToSelector:@selector(refreshControlPullDown:)]) {
            [self.delegate refreshControlPullDown:self];
        }
       
    }else{
        if ([self.delegate respondsToSelector:@selector(refreshControlPullUp:)]) {
            [self.delegate refreshControlPullUp:self];
        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y >= 0.0 && scrollView.contentOffset.y < 80) {
        [refreshControl finishRefreshingDirection:RefreshDirectionTop];
    }
    if (scrollView.contentOffset.y + scrollView.frame.size.height<= scrollView.contentSize.height && scrollView.contentOffset.y + scrollView.frame.size.height >= scrollView.contentSize.height - 80) {
        [refreshControl finishRefreshingDirection:RefreshDirectionBottom];
    }
    
    if (scrollView.contentOffset.y + scrollView.frame.size.height <= scrollView.contentSize.height+50) {
        refreshControl.isPullDown = YES;
    }else{
        refreshControl.isPullDown = NO;
    }
    
}

#pragma mark - subject cell delegate - 

-(void)checkStickerDetailsAction:(UIView *)view
{
    if ([self.delegate respondsToSelector:@selector(subjectCheckAction:)]) {
        [self.delegate subjectCheckAction:self];
    }
}

-(void)shareAction:(UIView *)view
{
    if ([self.delegate respondsToSelector:@selector(subjectShareAction:)]) {
        [self.delegate subjectShareAction:self];
    }
}



#pragma mark -setter-
- (void)setHotSpotDetailListArr:(NSArray *)infoArray
{
    @try {
        NSString * fileNameStr = [NSString stringWithFormat:@"%@.plist",currentHotSpotsItem.group_id];
        NSMutableDictionary * infoDic = [CTFile getDicFile:fileNameStr fromDoc:CTFileDocLibrary fromPath:@"cacheData/homepage/hotspotList"];
        
        if ([[NSString stringWithFormat:@"%@",[infoDic objectForKey:@"next_page"]] isEqualToString:@"1"])refreshControl.bottomEnabled = YES;
        else refreshControl.bottomEnabled = NO;
    }
    @catch (NSException *exception) {
        refreshControl.bottomEnabled = NO;
    }
    
    
    hotSpotsArray = nil;
    hotSpotsArray = [infoArray mutableCopy];
    [homePageTableView reloadData];
}

-(void)setSubjectArr:(CTSubjectItem *)subjItem
{
    subjectItm = subjItem;
    [homePageTableView reloadData];
}
#pragma mark - CTHOtSpotDetailCellDelegate -
/**
 *@brief 点击评论总数
 */
- (void)hotSpotDetailCellCommentCountAction:(CTHotSpotDetailCell *)cell withItem:(CTHotSpotsDetailItem *)item
{
    if ([self.delegate respondsToSelector:@selector(homePageTableViewCommentCountAction:withItem:)])
    {
        [self.delegate homePageTableViewCommentCountAction:self withItem:item];
    }
}
/**
 *@brief 点击评论第一条
 */
- (void)hotSpotDetailCellCommentHitFirstLabelAction:(CTHotSpotDetailCell *) cell withItem:(CTHotSpotsDetailItem *)item
{
    if ([self.delegate respondsToSelector:@selector(homePageTableViewCommentHitFirstLabelAction:withItem:)])
    {
        [self.delegate homePageTableViewCommentHitFirstLabelAction:self withItem:item];
    }

}
/**
 *@brief 点击评论第二条
 */
- (void)hotSpotDetailCellCommentHitSecondLabelAction:(CTHotSpotDetailCell *) cell withItem:(CTHotSpotsDetailItem *)item
{
    if ([self.delegate respondsToSelector:@selector(homePageTableViewCommentHitSecondLabelAction:withItem:)])
    {
        [self.delegate homePageTableViewCommentHitSecondLabelAction:self withItem:item];
    }

}
/**
 *@brief 点击评论第一条头像
 */
- (void)hotSpotDetailCellCommentHitFirstHeadAction:(CTHotSpotDetailCell *)cell withItem:(CTComment *) item
{
    if ([self.delegate respondsToSelector:@selector(homePageTableViewCommentHitFirstHeadAction:withItem:)])
    {
        [self.delegate homePageTableViewCommentHitFirstHeadAction:self withItem:item];
    }
}
/**
 *@brief 点击评论第二条头像
 */
- (void)hotSpotDetailCellCommentHitSecondHeadAction:(CTHotSpotDetailCell *)cell withItem:(CTComment *)item
{
    if ([self.delegate respondsToSelector:@selector(homePageTableViewCommentHitSecondHeadAction:withItem:)])
    {
        [self.delegate homePageTableViewCommentHitSecondHeadAction:self withItem:item];
    }

}
/**
 *@brief 点击头像
 */
- (void)hotSpotDetailCellHitHeadAction:(CTHotSpotDetailCell *)cell withItem:(CTHotSpotsDetailItem *)item
{
    if ([self.delegate respondsToSelector:@selector(homePageTableViewHitHeadAction:withItem:)])
    {
        [self.delegate homePageTableViewHitHeadAction:self withItem:item];
    }

}
/**
 *@brief 点击大图
 */
- (void)hotSpotDetailCellHitPicImageBtnAction:(CTHotSpotDetailCell *)cell withItem:(CTHotSpotsDetailItem *)item withImageView:(UIImageView *)imgView
{
    if ([self.delegate respondsToSelector:@selector(homePageTableViewHitPicImageBtnAction:withItem:withImageView:)])
    {
        [self.delegate homePageTableViewHitPicImageBtnAction:self withItem:item withImageView:imgView];
    }

}
/**
 *@brief 点赞
 */
- (void)hotSpotDetailCellHitFavourBtnAction:(CTHotSpotDetailCell *)cell withItem:(CTHotSpotsDetailItem *)item withBool:(BOOL) favBool
{
    CTListDetailUser *listItme = [[CTListDetailUser alloc]init];
    listItme.profile_image_url_thumb = [CTUser userInfo].profile_image_url_thumb;
    listItme.name = [CTUser userInfo].name;
    listItme.gender = [CTUser userInfo].gender;
    listItme.uid = [CTUser userInfo].uid;
    if (favBool == YES)
    {
        item.hotSpotsDetailItem.liked = @"1";
        NSMutableArray * itemArray =  [item.hotSpotsDetailItem.like_list mutableCopy];
        [itemArray addObject:listItme];
        item.hotSpotsDetailItem.like_list = nil;
        item.hotSpotsDetailItem.like_list = itemArray;
        itemArray = nil;
        if (item.hotSpotsDetailItem.like_list.count == 1)
        {
            [homePageTableView reloadData];
        }
    }
    else
    {
        item.hotSpotsDetailItem.liked = @"0";
       NSMutableArray * itemArray = [item.hotSpotsDetailItem.like_list mutableCopy];
        for (int i = 0; i <itemArray.count; i++)
        {
            CTListDetailUser *userItem = [itemArray objectAtIndex:i];
            if ([userItem.uid isEqualToString:listItme.uid])
            {
                if (itemArray.count ==1)
                {
                    [itemArray removeObjectAtIndex:i];
                    [homePageTableView reloadData];
                }
                else
                {
                    [itemArray removeObjectAtIndex:i];
                }
                break;
            }
        }
        item.hotSpotsDetailItem.like_list = nil;
        item.hotSpotsDetailItem.like_list = itemArray;
    }
    if ([self.delegate respondsToSelector:@selector(homePageTableViewHitFavBtnHeadAction:withItem:)])
    {
        [self.delegate homePageTableViewHitFavourBtnAction:self withItem:item withBool:favBool];
    }

}
/**
 *@brief 点击评论按钮
 */
- (void)hotSpotDetailCellHitCommentBtnaction:(CTHotSpotDetailCell *) cell withItem:(CTHotSpotsDetailItem *)item
{
    if ([self.delegate respondsToSelector:@selector(homePageTableViewHitCommentBtnaction:withItem:)]) {
        [self.delegate homePageTableViewHitCommentBtnaction:self withItem:item];
    }
}
/**
 *@brief 点击分享按钮
 */
- (void)hotSpotDetailCellHitShareBtnAction:(CTHotSpotDetailCell *) cell withItem:(CTHotSpotsDetailItem *)item withImage:(UIImage *)img
{
    if ([self.delegate respondsToSelector:@selector(homePageTableViewHitShareBtnAction:withItem:withImage:)])
    {
        [self.delegate homePageTableViewHitShareBtnAction:self withItem:item withImage:img];
    }
}
/**
 *@brief 点击更多按钮
 */
- (void)hotSpotDetailCellHitMoreBtnAction:(CTHotSpotDetailCell *) cell withItem:(CTHotSpotsDetailItem *)item
{
    if ([self.delegate respondsToSelector:@selector(homePageTableViewHitMoreBtnAction:withItem:)]) {
        [self.delegate homePageTableViewHitMoreBtnAction:self withItem:item];
    }
}
/**
 *@brief 点击点赞的人的头像
 */
- (void)hotSpotDetailCellHitFavBtnHeadAction:(CTHotSpotDetailCell *)cell withItem:(CTListDetailUser *)item
{
    if ([self.delegate respondsToSelector:@selector(homePageTableViewHitFavBtnHeadAction:withItem:)])
    {
        [self.delegate homePageTableViewHitFavBtnHeadAction:self withItem:item];
    }

}
/**
 *@brief 点击关注按钮
 */
- (void)hotSpotDetailCellHitAttentionBtnAction:(CTHotSpotDetailCell *) cell withItem:(CTHotSpotsDetailItem *)item withBool:(BOOL)attentionBool
{
    if (attentionBool == YES)
    {
        item.hotSpotsDetailItem.user.following = @"1";
    }
    else
    {
        item.hotSpotsDetailItem.user.following = @"0";
    }
    if ([self.delegate respondsToSelector:@selector(homePageTableViewHitAttentionBtnAction:withItem:withBool:)])
    {
        [self.delegate homePageTableViewHitAttentionBtnAction:self withItem:item withBool:attentionBool];
    }
}
/**
 *@brief 点击话题
 */
- (void)hotSpotDetailCellHitTopicBtnAction:(CTHotSpotDetailCell *) cell withItem:(CTTopic *)item
{
    if ([self.delegate respondsToSelector:@selector(homePageTableViewHitTopicBtnAction:withItem:)])
    {
        [self.delegate homePageTableViewHitTopicBtnAction:self withItem:item];
    }

}
@end
